package io.gitee.xyuesheng.day07;

import org.junit.Test;

import java.io.File;

public class HomeworkRecursion {
    //递归的遍历一个目录,按文件大小排序（目录总是排到最前面）
    @Test
    public void testFileIterator3() {
        String dirr = "D:\\RJ\\java\\src";//当前目录
        listDirectory(new File(dirr));
    }

    //传入一个目录，返回目录下面的子目录和文件
    public void listDirectory(File file) {
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {//是目录
                System.out.println("*目录*" + files[i].getAbsolutePath() + "#*#");
                listDirectory(files[i]);
            }
        }
        for (int i = 0; i < files.length; i++) {
            int m = (int) (files[i].length());
//          System.out.print(m + "#");

            int min = i;

            for (int j = i + 1; j < files.length; j++) {
                int n = (int) (files[j].length());
//              System.out.print(n + "#");
                if (n < m) {
                    min = j;
                }
            }
            if (min != i) {
                int temp = i;
                i = min;
                min = temp;
            }
            System.out.println(files[i].length() + "#" + files[i].getAbsolutePath());
        }

    }


}


